package com.emokili.ewa.app.service.impl;

import com.emokili.ewa.app.service.MemberAppService;
import com.emokili.ewa.common.utils.JsonUtils;
import com.emokili.ewa.common.vo.MemberLoginVo;
import com.emokili.ewa.core.frame.entity.MemberEntity;
import com.emokili.ewa.core.frame.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Service
public class MemberAppServiceImpl implements MemberAppService {
    @Autowired
    private MemberService memberService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${REDIS_USER_SESSION_KEY}")
    private String REDIS_USER_SESSION_KEY;

    @Value("${SSO_SESSION_EXPIRE}")
    private Integer SSO_SESSION_EXPIRE;

    @Override
    public String login(MemberLoginVo memberLoginVo) {
        MemberEntity memberEntity = memberService.login(memberLoginVo);
        //生成token
        String token = UUID.randomUUID().toString();
        //保存用户之前，把用户对象中的密码清空。
        memberEntity.setPassword(null);
        stringRedisTemplate.opsForValue().set(REDIS_USER_SESSION_KEY + ":" + token,
                JsonUtils.objectToJson(memberEntity),SSO_SESSION_EXPIRE, TimeUnit.MINUTES);
        //添加写cookie的逻辑，cookie的有效期是关闭浏览器就失效。
        // CookieUtils.setCookie(request, response, "TT_TOKEN", token);
        return token;
    }
}
